home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / bin / autopartition-loop < prev    next >
Text File  |  2009-01-15  |  9KB  |  375 lines

  1. #! /bin/sh
  2.  
  3. . /lib/partman/lib/base.sh
  4. . /lib/partman/lib/resize.sh
  5. . /lib/partman/lib/recipes.sh
  6.  
  7. # busybox wants mount -o move; util-linux wants mount --move. Sigh.
  8. if [ -d /lib/debian-installer ]; then
  9.     mount_move='-o move'
  10. else
  11.     mount_move='--move'
  12. fi
  13.  
  14. modprobe loop >/dev/null 2>&1 || true
  15.  
  16. # Set up working directory.
  17.  
  18. if type mktemp >/dev/null 2>&1; then
  19.     recipe_dir="$(mktemp -d /tmp/partman-auto-loop.XXXXXX)"
  20.     trap "rm -rf $recipe_dir" EXIT HUP INT QUIT TERM
  21. else
  22.     recipe_dir=/tmp
  23. fi
  24.  
  25. # Fetch parameters.
  26.  
  27. disk="$1"
  28.  
  29. cd $disk
  30.  
  31. if ! db_get partman-auto-loop/partition || [ -z "$RET" ]; then
  32.     logger -t partman-auto-loop "Error: No partition number specified in partman-auto-loop/partition"
  33.     exit 1
  34. fi
  35. partition="$RET"
  36.  
  37. if ! db_get partman-auto-loop/recipe || [ -z "$RET" ]; then
  38.     logger -t partman-auto-loop "Error: No recipe specified in partman-auto-loop/recipe"
  39.     exit 1
  40. fi
  41. recipe="$RET"
  42. echo "$recipe" >"$recipe_dir/loop_recipe"
  43.  
  44. # Find the requested partition.
  45.  
  46. db_progress START 0 5 partman-auto/text/automatically_partition
  47. db_progress INFO partman-auto/progress/info
  48.  
  49. partition_id=
  50. partition_fs=
  51. open_dialog PARTITIONS
  52. while { read_line num id size type fs path name; [ "$id" ]; }; do
  53.     if [ "$num" = "$partition" ]; then
  54.         partition_id="$id"
  55.         partition_fs="$fs"
  56.         # go ahead and read all remaining input
  57.     fi
  58. done
  59. close_dialog
  60. if [ -z "$partition_id" ]; then
  61.     logger -t partman-auto-loop "Error: Partition number $partition not found in $disk"
  62.     exit 1
  63. fi
  64.  
  65. # Set up the requested partition in partman.
  66.  
  67. existing=no
  68. for j in $(
  69.     for i in /lib/partman/valid_filesystems/*; do
  70.         [ -x $i ] || continue
  71.         $i $disk $partition_id existing
  72.     done
  73. ); do
  74.     if [ "$j" = "$partition_fs" ]; then
  75.         existing=yes
  76.     fi
  77. done
  78.  
  79. if [ "$existing" = no ]; then
  80.     logger -t partman-auto-loop "Error: No filesystem on $disk/$partition_id"
  81.     exit 1
  82. fi
  83.  
  84. echo keep >$partition_id/method
  85. rm -f $partition_id/format
  86. >$partition_id/use_filesystem
  87. echo $partition_fs >$partition_id/filesystem
  88. mkdir -p $partition_id/options
  89. echo / >$partition_id/mountpoint
  90. update_partition $disk $partition_id
  91.  
  92. # Is there enough space to perform the recipe?
  93.  
  94. dev="$disk"
  95. oldid="$partition_id"
  96.  
  97. recipe_new=
  98. firstword=
  99. imagepaths=
  100. for word in $(cat "$recipe_dir/loop_recipe"); do
  101.     case $word in
  102.         .)
  103.             recipe_new="${recipe_new:+$recipe_new }\$imagepath{ $firstword } $word"
  104.             firstword=
  105.             ;;
  106.         *)
  107.             if [ "$firstword" ]; then
  108.                 recipe_new="${recipe_new:+$recipe_new }$word"
  109.             else
  110.                 firstword="$word"
  111.                 imagepaths="${imagepaths:+$imagepaths }$word"
  112.             fi
  113.             ;;
  114.     esac
  115. done
  116. echo "$recipe_new" >"$recipe_dir/loop_recipe_new"
  117. decode_recipe "$recipe_dir/loop_recipe_new" loop
  118.  
  119. db_progress STEP 1
  120.  
  121. fstab="$(
  122.     for i in /lib/partman/fstab.d/*; do
  123.         [ -x "$i" ] || continue
  124.         $i
  125.     done |
  126.     while read fs mp type options dump pass; do
  127.         case $mp in
  128.             (/)
  129.                 echo $fs $mp $type $options $dump $pass
  130.                 ;;
  131.         esac
  132.     done
  133. )"
  134.  
  135. if [ -z "$fstab" ]; then
  136.     logger -t partman-auto-loop "Error: No fstab output for $disk/$partition_id"
  137.     exit 1
  138. fi
  139.  
  140. mkdir -p /target
  141.  
  142. mountpoint="$(grep "^${fstab%% *} [^ ]* [^ ]* [^ ]*rw" /proc/mounts | cut -d ' ' -f2 | head -n1)" || mountpoint=
  143. if [ "$mountpoint" = /target ]; then
  144.     # nothing to do
  145.     :
  146. elif [ "$mountpoint" ]; then
  147.     if ! mount $mount_move "$mountpoint" /target; then
  148.         logger -t partman-auto-loop "Error: Failed to move $mountpoint to /target"
  149.         exit 1
  150.     fi
  151.     unmount_cmd='umount /target'
  152. else
  153.     for m in /lib/partman/mount.d/*; do
  154.         [ -x "$m" ] || continue
  155.  
  156.         unmount_cmd="$($m "$fstab")"
  157.         if [ "$?" = 0 ]; then
  158.             break
  159.         fi
  160.     done
  161. fi
  162. if [ -d /var/run ]; then
  163.     pidof mount.ntfs >> /var/run/sendsigs.omit
  164.     pidof mount.ntfs-3g >> /var/run/sendsigs.omit
  165. fi
  166.  
  167. # TODO: handle errors if no mount succeeded
  168. mkdir -p /host
  169. mount $mount_move /target /host # TODO error handling
  170.  
  171. # Don't try to mount this again later.
  172. rm -f $partition_id/mountpoint
  173.  
  174. # Ensure there is enough free space.
  175. check_free_space=false
  176. requires_disk_space(){
  177.     [ "$1" != 0 ] || return
  178.     path="$(echo "$*" | sed 's/.*\$imagepath{  *\([^ }]*\) *}.*/\1/')"
  179.     [ "$path" != "$*" ] || return
  180.     case $path in
  181.         /*)    ;;
  182.         *)    path="/$path" ;;
  183.     esac
  184.     [ -f "/host$path" ] && return 
  185.     check_free_space=true
  186. }
  187. foreach_partition 'requires_disk_space $*' 
  188.  
  189. # Skip resize_range check if images are already created.
  190. if [ $check_free_space = true ]; then
  191.     case $partition_fs in
  192.     linux-swap|fat16|fat32|hfs|hfs+|hfsx)
  193.         get_resize_range
  194.         ;;
  195.     ext2|ext3|ext4)
  196.         if ! search-path tune2fs; then
  197.             logger -t partman-auto-loop "Error: tune2fs not found"
  198.             exit 1
  199.         fi
  200.         if ! search-path resize2fs; then
  201.             logger -t partman-auto-loop "Error: resize2fs not found"
  202.             exit 1
  203.         fi
  204.         if ! get_ext2_resize_range; then
  205.             logger -t partman-auto-loop "Error: Failed to get ext2 resize range for $disk/$partition_id"
  206.             exit 1
  207.         fi
  208.         ;;
  209.     ntfs)
  210.         if ! search-path ntfsresize; then
  211.             logger -t partman-auto-loop "Error: ntfsresize not found"
  212.             exit 1
  213.         fi
  214.         if ! get_ntfs_resize_range; then
  215.             db_input critical partman-auto-loop/unclean_ntfs || true
  216.             db_go || true
  217.             logger -t partman-auto-loop "Error: Failed to get NTFS resize range for $disk/$partition_id"
  218.             reboot
  219.             exit 1
  220.         fi
  221.         ;;
  222.     *)
  223.         logger -t partman-auto-loop "Cannot calculate free space on filesystems of type $partition_fs"
  224.         exit 1
  225.         ;;
  226.     esac
  227.     free_size="$(expr \( "$cursize" - "$minsize" \) \* 9 / 10)"
  228.     # convert to megabytes
  229.     free_size="$(expr 0000000"$free_size" : '0*\(..*\)......$')"
  230.     if [ $(min_size) -gt $free_size ]; then
  231.         logger -t partman-auto-loop "Error: partman-auto-loop/recipe too large ($(min_size) > $free_size)"
  232.         exit 1
  233.     fi
  234. fi
  235.  
  236. # Ensure that no old loop images are present and mountable.
  237. found_images=
  238. mkdir -p /tmpmountpoint
  239. for path in $imagepaths; do
  240.     case $path in
  241.         /*)    ;;
  242.         *)    path="/$path" ;;
  243.     esac
  244.     if [ -e "/host$path" ]; then
  245.         if mount -t auto -o loop,ro /host$path /tmpmountpoint 2>/dev/null; then
  246.             found_images="${found_images:+$found_images }$path"
  247.             umount /tmpmountpoint || true
  248.             rmdir /tmpmountpoint || true
  249.         fi
  250.     fi
  251. done
  252. if [ "$found_images" ]; then
  253.     db_progress STOP
  254.     db_subst partman-auto-loop/unclean_host PARTITION "$partition"
  255.     db_subst partman-auto-loop/unclean_host DISK "$disk"
  256.     db_subst partman-auto-loop/unclean_host IMAGES "$found_images"
  257.     db_input critical partman-auto-loop/unclean_host || true
  258.     db_capb
  259.     db_go || true
  260.     db_capb backup
  261.     umount /host || true
  262.     exit 1
  263. fi
  264.  
  265. db_progress STEP 1
  266.  
  267. expand_scheme
  268.  
  269. db_progress STEP 1
  270.  
  271. clean_method
  272.  
  273. db_progress STEP 1
  274.  
  275. setup_loop () {
  276.     [ "$1" != 0 ] || return
  277.     path="$(echo "$*" | sed 's/.*\$imagepath{  *\([^ }]*\) *}.*/\1/')"
  278.     [ "$path" != "$*" ] || return
  279.  
  280.     case $path in
  281.         /*)    ;;
  282.         *)    path="/$path" ;;
  283.     esac
  284.     if [ ! -f "/host$path" ]; then
  285.         mkdir -p "/host${path%/*}"
  286.         if [ "$4" = "linux-swap" ]; then
  287.             # swap requires a file with no holes
  288.             dd if=/dev/zero of="/host$path" bs="1000000" count="$1"
  289.         else
  290.             dd if=/dev/zero of="/host$path" bs="1000000" seek="$1" count=0
  291.         fi
  292.     fi
  293.     if ! losetup -f "/host$path"; then
  294.         shift
  295.         continue
  296.     fi
  297.     if [ "$4" = linux-swap ]; then
  298.         loops="/host$path"
  299.     else
  300.         loops="$(echo /dev/loop* /dev/loop/*)"
  301.     fi
  302.     for loop in $loops; do
  303.         [ -e "$loop" ] || continue
  304.         case $loop in
  305.             /dev/loop*)
  306.                 loopfile="$(losetup "$loop")" || continue
  307.                 # The following works with both busybox's
  308.                 # losetup and util-linux's losetup. Yes,
  309.                 # this is ugly.
  310.                 loopfile="$(echo "$loopfile" | sed 's,.*\(/host/[^)]*\).*,\1,')"
  311.                 ;;
  312.             *)
  313.                 loopfile="$loop"
  314.                 ;;
  315.         esac
  316.         [ "$loopfile" = "/host$path" ] || continue
  317.  
  318.         dirname="$(echo "$loop" | sed 's:/:=:g')"
  319.         dev="$DEVICES/$dirname"
  320.  
  321.         rm -rf "$dev"
  322.         mkdir "$dev" || autopartitioning_failed
  323.         printf "%s" "$loop" >"$dev/device"
  324.         printf "%s" "$1" >"$dev/size"
  325.         echo "Loopback on $loopfile" >"$dev/model"
  326.         echo "$loopfile" >"$dev/loop"
  327.  
  328.         cd "$dev"
  329.         open_dialog OPEN "$(cat "$dev/device")"
  330.         read_line response
  331.         close_dialog
  332.         if [ "$response" = failed ]; then
  333.             cd /
  334.             rm -rf "$dev"
  335.             autopartitioning_failed
  336.         fi
  337.  
  338.         open_dialog NEW_LABEL loop
  339.         close_dialog
  340.  
  341.         # find the free space
  342.         open_dialog PARTITIONS
  343.         free_space=
  344.         while { read_line num id size type fs path name; [ "$id" ]; }; do
  345.             if [ "$fs" = free ]; then
  346.                 free_space=$id
  347.                 free_size=$size
  348.             fi
  349.         done
  350.         close_dialog
  351.  
  352.         # create partition in the free space
  353.         [ "$free_space" ] || autopartitioning_failed
  354.         open_dialog NEW_PARTITION primary $4 $free_space full ${1}000001
  355.         read_line num id size type fs path name
  356.         close_dialog
  357.  
  358.         shift; shift; shift; shift
  359.         setup_partition $id $*
  360.         break
  361.     done
  362. }
  363.  
  364. foreach_partition 'setup_loop $*'
  365.  
  366. db_progress STEP 1
  367.  
  368. update_all
  369.  
  370. apt-install lupin-support
  371.  
  372. db_progress STOP
  373.  
  374. exit 0
  375.